package nayasis.common.conf;

import java.io.IOException;
import java.util.List;

import nayasis.common.Const;
import nayasis.common.log.NLogger;
import nayasis.common.xml.XmlBuilder;

import org.jdom2.Attribute;
import org.jdom2.Element;
import org.jdom2.JDOMException;

/**
 * Configulator to read configulation file ( ./resource/conf/common.xml )
 *
 */
public abstract class NConfigulator {

    private static XmlBuilder conf;

    static {
        
        try {
            conf = new XmlBuilder( Const.path.conf + "/common.xml" );
        } catch( IOException | JDOMException e ) {
            NLogger.err.println( e );
        }
    
    }

    public static List<Element> getElements( String xPath ) {
        return conf.getElements( xPath );
    }

    public static Element getElement( String xPath ) {
        return conf.getElement( xPath );
    }
    
    public static List<Attribute> getAttributes( String xPath ) {
        return conf.getAttributes( xPath );
    }

    public static Attribute getAttribute( String xPath ) {
        return conf.getAttribute( xPath );
    }
    
    public static String getValue( Element element ) {
        
        String val = conf.getValue( element );
        
        if( val != null ) {
            val = val.replaceAll( "#resource#", Const.path.getHome() );
        }
        
        return val;
    }

    public static String getValue( Attribute attribute ) {

        String val = conf.getValue( attribute );
        
        if( val != null ) {
            val = val.replaceAll( "#resource#", Const.path.getHome() );
        }
        
        return val;
        
    }

    public static String getElementValue( String xPath ) {
        return getValue( getElement(xPath) );
    }
    
    
    public String getAttributeValue( String xPath ) {
        return getValue( getAttribute(xPath) );
    }

}
